import { createRouter, createWebHistory } from 'vue-router'
import cookie from 'js-cookie'//引入处理cookies的Api

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/index/dashboard'
    },
    {
      path: '/index',
      redirect: '/index/dashboard'
    },
    {
      path: '/index',
      name: 'IndexView',
      component: () => import('../views/Index.vue'),
      children: [{
        path: 'userinfo',
        name: 'userinfo',
        component: () => import('@/views/admin/UserInfo.vue'),
        meta: { title: 'userinfo' }
      }, {
        path: 'revisePwd',
        name: 'revisePwd',
        component: () => import('@/views/admin/RevisePwd.vue'),
        meta: { title: 'revisePwd' }
      }, {
        path: 'redirect/:backpath(.*)',
        name: 'redirect',
        meta: { title: 'home' },//路由携带信息
        component: () => import('../views/redirect/index.vue')
      }]
    },
    {
      path: '/login',
      name: 'LoginView',
      component: () => import('../views/Login.vue')
    },
    {
      path: '/404',
      name: 'NoteFoundView',
      component: () => import('../views/404.vue')
    }
  ]
})
// 路由全局守卫
router.beforeEach((to, from) => {
  const token = cookie.get('token')
  if (!token && to.path != '/login') { //判断cook存储里没有token头,并且排除掉登录页面
    // 携带当前页面路由，以便在登录页面完成登录后返回上一次登陆成功后跳转的页面
    router.replace({ path: '/login', query: { back: to.fullPath } })
    return false
  }
  return true
})


export default router
